package com.mcxx.modules.statistics.dto;

import lombok.Data;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

import static java.util.stream.Collectors.toList;


@Data
public class MiniLivingAuditSummaryDTO extends StatisticDTO implements Serializable {

    private String areaCode;
    private String area;
    private Integer houseNum;
    private Integer members;
    private Double basicSalvage;
    private Double categorySalvage;
    private Double reissueSalvage;
    private Double customerSalvage;
    private Double totalSalvage;
    private Integer all_houseNum;
    private Integer all_members;
    private Double all_basicSalvage;
    private Double all_categorySalvage;
    private Double all_reissueSalvage;
    private Double all_customerSalvage;
    private Double all_totalSalvage;
    private Integer one_houseNum;
    private Integer one_members;
    private Double one_basicSalvage;
    private Double one_categorySalvage;
    private Double one_reissueSalvage;
    private Double one_customerSalvage;
    private Double one_totalSalvage;
    private String title;
    private String isHe;


    public static MiniLivingAuditSummaryDTO dealWithAllAndSingleSave(List<MinLivingDetailDTO> saveList,MiniLivingAuditSummaryDTO dto){
        dto.setHouseNum(getFamilyNum(saveList));
        Integer members = saveList.stream().mapToInt(MinLivingDetailDTO::getMembers).sum();
        dto.setMembers(members);
        Double basicSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getBasicSalvage).sum();
        dto.setBasicSalvage(basicSalvage);
        Double categorySalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getCategorySalvage).sum();
        dto.setCategorySalvage(categorySalvage);
        Double reissueSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getReissueSalvage).sum();
        dto.setReissueSalvage(reissueSalvage);
        Double customerSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getCustomerSalvage).sum();
        dto.setCustomerSalvage(customerSalvage);
        dto.setTotalSalvage(basicSalvage + categorySalvage + reissueSalvage + customerSalvage);
        return dto;
    }


    public static MiniLivingAuditSummaryDTO dealWithAllSave(List<MinLivingDetailDTO> saveList,MiniLivingAuditSummaryDTO dto){
        if (saveList != null && saveList.size() > 0) {
            Integer members = saveList.stream().mapToInt(MinLivingDetailDTO::getMembers).sum();
            Double basicSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getBasicSalvage).sum();
            Double categorySalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getCategorySalvage).sum();
            Double reissueSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getReissueSalvage).sum();
            Double customerSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getCustomerSalvage).sum();
            dto.setAll_houseNum(getFamilyNum(saveList));
            dto.setAll_members(members);
            dto.setAll_basicSalvage(basicSalvage);
            dto.setAll_categorySalvage(categorySalvage);
            dto.setAll_reissueSalvage(reissueSalvage);
            dto.setAll_customerSalvage(customerSalvage);
            dto.setAll_totalSalvage(basicSalvage + categorySalvage + reissueSalvage + customerSalvage);
        } else {
            dto.setAll_basicSalvage(0d);
            dto.setAll_categorySalvage(0d);
            dto.setAll_reissueSalvage(0d);
            dto.setAll_houseNum(0);
            dto.setAll_members(0);
            dto.setAll_customerSalvage(0d);
            dto.setAll_totalSalvage(0d);
        }
        return dto;
    }


    public static MiniLivingAuditSummaryDTO dealWithSingleSave(List<MinLivingDetailDTO> saveList,MiniLivingAuditSummaryDTO dto){
        if (saveList != null && saveList.size() > 0) {
            Integer members = saveList.stream().mapToInt(MinLivingDetailDTO::getMembers).sum();
            Double basicSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getBasicSalvage).sum();
            Double categorySalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getCategorySalvage).sum();
            Double reissueSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getReissueSalvage).sum();
            Double customerSalvage = saveList.stream().mapToDouble(MinLivingDetailDTO::getCustomerSalvage).sum();
            dto.setOne_houseNum(getFamilyNum(saveList));
            dto.setOne_members(members);
            dto.setOne_basicSalvage(basicSalvage);
            dto.setOne_categorySalvage(categorySalvage);
            dto.setOne_reissueSalvage(reissueSalvage);
            dto.setOne_customerSalvage(customerSalvage);
            dto.setOne_totalSalvage(basicSalvage + categorySalvage + reissueSalvage + customerSalvage);
        } else {
            dto.setOne_basicSalvage(0d);
            dto.setOne_categorySalvage(0d);
            dto.setOne_reissueSalvage(0d);
            dto.setOne_houseNum(0);
            dto.setOne_members(0);
            dto.setOne_customerSalvage(0d);
            dto.setOne_totalSalvage(0d);
        }
        return dto;
    }

    private static Integer getFamilyNum(List<MinLivingDetailDTO> saveList) {
        List<String> familyIds = saveList.stream().map(MinLivingDetailDTO::getFamilyId).collect(toList());

        List<String> noRepeat = familyIds.stream().distinct().collect(toList());

        List<String> notNull = noRepeat.stream().filter(e -> Objects.nonNull(e)).collect(toList());
        return notNull.size();
    }
}
